//
//  MainTopicTableView.m
//  ZITHER
//
//  Created by 吴凡 on 2019/2/14.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "MainTopicTableView.h"
#import "MainTopicContentCell.h"
#import "MainTopicSingleImageCell.h"
#import "MainTopicMulImageCell.h"
#import "MainTopicShortCell.h"
#import "MainTopicExprieCellTableViewCell.h"

#import "CRBBoxHomeHttp.h"
#import "MainTopicModel.h"
#import "TopicSubModel.h"
#import "TopicUserSubjectImagesModel.h"
#import "TopicUgcModel.h"

//话题详情
#import "TopicDetailViewController.h"
#import "MainTopicViewController.h"

@interface MainTopicTableView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)MainTopicModel *mainTopicModel;
@property (nonatomic,strong)MainTopicModel *expireTopicModel;
@property (nonatomic,strong)NSMutableArray *mainDataSource;
@property (nonatomic,strong)NSMutableArray *expireDataSource;
@property (nonatomic,assign)BOOL isLastPage; // 未过期的最后一页
@property (nonatomic,assign)NSInteger expirePage; // 过期话题的页数

@end

@implementation MainTopicTableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.delegate = self;
        self.dataSource = self;
        self.page = 1;
        self.expirePage = 0; // foot后会++ 变成1
        self.isLastPage = NO;
        self.mainTopicModel = [MainTopicModel new];
        self.expireTopicModel = [MainTopicModel new];
        self.mainDataSource = [NSMutableArray new];
        self.expireDataSource = [NSMutableArray new];
        [self Starefreshing];
        
        @weakify(self);
        self.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            @strongify(self);
            self.isLastPage?self.expirePage++:self.page++;
            [self Starefreshing];
        }];
        self.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            @strongify(self);
            self.page = 1;
            self.expirePage = 0;
            self.isLastPage = NO;
            [self Starefreshing];
        }];
    }
    return self;
}
- (void)Starefreshing {
    if (!_isLastPage) {
        [CRBBoxHomeHttp requestTopicListWithPage:@(self.page) size:@(8) success:^(NSURLSessionDataTask *task, id responseObject) {
            if (self.page == 1) {
                [self.mainDataSource removeAllObjects];
                [self.expireDataSource removeAllObjects];
            }
            self.mainTopicModel = (MainTopicModel *)responseObject;
            self.page = self.mainTopicModel.pageNum;
            if (self.mainTopicModel.pages == self.mainTopicModel.pageNum) {
                self.isLastPage = YES;
            }
            for (TopicSubModel *subModel in self.mainTopicModel.list) {
                [self.mainDataSource addObject:subModel];
            }
            [self reloadData];
            [self endRefreshing];
        } fail:^(NSURLSessionDataTask *task, NSError *error, NSString *code, NSString *msg) {
            [Tools showTips:msg];
            [self endRefreshing];
        }];
    } else {
        [CRBBoxHomeHttp requestExpiredTopicListWithPage:@(self.expirePage) size:@(8) success:^(NSURLSessionDataTask *task, id responseObject) {
            self.expireTopicModel = (MainTopicModel *)responseObject;
            self.expirePage = self.expireTopicModel.pageNum;
            if ( (self.expireTopicModel.list.count == 0)) {
                [Tools showTips:@"暂无更多数据"];
                [self.mj_footer endRefreshingWithNoMoreData];
//                [self reloadData];
//                [self endRefreshing];
                return ;
            }
            for (TopicSubModel *subModel in self.expireTopicModel.list) {
                [self.expireDataSource addObject:subModel];
            }
            [self reloadData];
            [self endRefreshing];
        } fail:^(NSURLSessionDataTask *task, NSError *error, NSString *code, NSString *msg) {
            [Tools showTips:msg];
            [self endRefreshing];
        }];
    }
}

#pragma mark -- tabdelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.isLastPage) {
        return 2;
    } else {
        return 1;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.mainDataSource.count;
    } else if (section == 1) {
        return self.expireDataSource.count;
    }
    return 0;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        TopicSubModel *model = self.mainDataSource[indexPath.row];
        NSInteger type = 4;
        if (!model.userUgc) {
            type = 4; // 无ugc
        } else {
            if (!model.userUgc.fileList || model.userUgc.fileList.count == 0) {
                type = 1; // 有ugc 无图片
            } else {
                if (model.userUgc.fileList.count == 1) {
                    type = 2; // 有ugc 单图
                } else if (model.userUgc.fileList.count > 1) {
                    type = 3; // 有ugc 多图
                }
            }
        }
        switch (type) {
            case 1:
            {
                static NSString *sectionCell = @"MainTopicContentCell";
                MainTopicContentCell *cell = [tableView dequeueReusableCellWithIdentifier:sectionCell];
                if (cell == nil) {
                    cell = [[MainTopicContentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
                }
                cell.selectionStyle = UITableViewCellSeparatorStyleNone;
                [cell setModel:model];
                return cell;
            }
                break;
            case 2:
            {
                static NSString *sectionCell = @"MainTopicSingleImageCell";
                MainTopicSingleImageCell *cell = [tableView dequeueReusableCellWithIdentifier:sectionCell];
                if (cell == nil) {
                    cell = [[MainTopicSingleImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
                }
                cell.selectionStyle = UITableViewCellSeparatorStyleNone;
                [cell setModel:model];
                return cell;
            }
                break;
            case 3:
            {
                static NSString *sectionCell = @"MainTopicMulImageCell";
                MainTopicMulImageCell *cell = [tableView dequeueReusableCellWithIdentifier:sectionCell];
                if (cell == nil) {
                    cell = [[MainTopicMulImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
                }
                cell.selectionStyle = UITableViewCellSeparatorStyleNone;
                [cell setModel:model];
                return cell;
            }
                break;
            case 4:
            {
                static NSString *sectionCell = @"MainTopicShortCell";
                MainTopicShortCell *cell = [tableView dequeueReusableCellWithIdentifier:sectionCell];
                if (cell == nil) {
                    cell = [[MainTopicShortCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
                }
                cell.selectionStyle = UITableViewCellSeparatorStyleNone;
                [cell setModel:model];
                cell.contentView.alpha = 1.f;
                return cell;
            }
                break;
            default:
                break;
        }
    }
    
    if (indexPath.section == 1) {
        
            TopicSubModel *model = self.expireDataSource[indexPath.row];
            static NSString *sectionCell = @"MainTopicShortCell";
            MainTopicShortCell *cell = [tableView dequeueReusableCellWithIdentifier:sectionCell];
            if (cell == nil) {
                cell = [[MainTopicShortCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
            }
            cell.selectionStyle = UITableViewCellSeparatorStyleNone;
            [cell setModel:model];
            cell.contentView.alpha = 0.5;
            return cell;

    }
    
    return nil;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TopicSubModel *model ;
    if (indexPath.section == 0) {
        model = self.mainDataSource[indexPath.row];
    } else if (indexPath.section == 1 ) {
        model = self.expireDataSource[indexPath.row];
    } else {
        return;
    }
    TopicDetailViewController *detailVC = [TopicDetailViewController new];
    detailVC.topicId = model.id;
     MainTopicViewController *vc  = (MainTopicViewController*)[self getViewController:[MainTopicViewController class]];
    [vc.navigationController pushViewController:detailVC animated:YES];
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 1 && self.isLastPage) {
        UIView* customView =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, kScreenW, 18)] ;
        customView.backgroundColor=[UIColor clearColor];
        UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectZero] ;
        titleLabel.text = @"已关闭的话题";
        titleLabel.textColor = [UIColor colorWithHex:@"#666666"];
        titleLabel.font = [UIFont systemFontOfSize:12];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [customView addSubview:titleLabel];
        UIView *leftLine = [UIView new];
        leftLine.backgroundColor = [UIColor colorWithHex:@"#EEEEEE"];
        [customView addSubview:leftLine];
        UIView *rightLine = [UIView new];
        rightLine.backgroundColor = [UIColor colorWithHex:@"#EEEEEE"];
        [customView addSubview:rightLine];

        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(customView);
        }];
        [leftLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0.5);
            make.left.equalTo(customView.mas_left).offset(15);
            make.right.equalTo(titleLabel.mas_left).offset(-25);
            make.centerY.equalTo(titleLabel);
        }];
        [rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0.5);
            make.right.equalTo(customView.mas_right).offset(-15);
            make.left.equalTo(titleLabel.mas_right).offset(25);
            make.centerY.equalTo(titleLabel);
        }];

        return customView;
    } else {
        return [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenH, 1)];
    }

}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1 && self.isLastPage) {
        return 18;
    } else {
        return 0;
    }
}
@end
